<div class="widget editor-widget">
	<div class="widget-header bg-blue">
		<i class="widget-icon icon-notebook"></i> 
		<span class="widget-caption"> 客户跟进 </span>
		<div class="widget-buttons">
			<a href="javascript:void(0);" onclick="handlerCancel(this);" title="关闭"
				 data-toggle="dispose" data-refresh-table="#table-allCustomer"> <i class="fa fa-times"></i> </a> 
		</div>
	</div>
	<div class="widget-body">
		<!-- TODO 跟进记录 -->
		<div class="widget radius-bordered mgbt-xs-10" id="editor-panel-selling">
			<div class="widget-header bg-gold">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> <a href="" data-toggle="collapse"> 跟进记录 </a> </span>
				<div class="widget-buttons buttons-bordered">
					<button class="btn btn-info btn-sm" title="添加" 
									data-toggle="tooltip" data-placement="top" 
									onclick="handlerAddRow('#table-selling')">
						<i class="fa fa-plus"></i>
					</button>
				</div>
				<div class="widget-buttons compact">
					<a href="" data-toggle="collapse"> <i class="fa fa-compress"></i> </a> 
				</div>
			</div>
			<div class="widget-body editor-body-sm">
				<form class="table-editor table-editor-selling" method="post">
					<table data-ajax="handlerSearch" data-method="post"
								 data-id-field="id" 
								 data-unique-id="id" 
								 data-pagination="true"  
								 data-check-on-init="true" 
								 data-side-pagination="server" 
								 data-mobile-responsive="true" 
								 data-form-source=".table-editor-selling" 
								 data-form-target="#editor-form-selling" 
								 data-form-search="#search-form-selling" id="table-selling">
						<thead>
							<tr>
								<th data-checkbox="true"></th>
								<th data-field="id" data-visible="false"></th>
								<th data-field="createDate" data-align="center">通话时间</th>
								<th data-field="intention" 	data-align="center" data-formatter="renderIntention">意向程度</th>
								<th data-field="content" 		data-align="center">跟踪情况</th>
								<th data-field="nextDate" 	data-align="center">下次沟通时间</th>
								<th data-field="username" 	data-align="center">顾问</th>
								<th data-field="userid" 		data-align="center" data-visible="false">顾问ID</th>
								<th data-field="operator" 	data-align="center" data-width="30" data-formatter="renderOperator" data-table-target="selling">操作</th>
							</tr>
						</thead>
					</table>
				</form>
			</div>
		</div>
		<!-- TODO 诺访记录 -->
		<div class="widget radius-bordered mgbt-xs-10" id="editor-panel-bespoke">
			<div class="widget-header bg-gold">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> <a href="" data-toggle="collapse"> 诺访记录 </a> </span>
				<div class="widget-buttons buttons-bordered">
					<button class="btn btn-info btn-sm" title="添加" 
									data-toggle="tooltip" data-placement="top" 
									onclick="intercept('#table-bespoke')">
						<i class="fa fa-plus"></i>
					</button>
				</div>
				<div class="widget-buttons compact">
					<a href="" data-toggle="collapse"> <i class="fa fa-compress"></i> </a> 
				</div>
			</div>
			<div class="widget-body editor-body-sm">
				<form class="table-editor table-editor-bespoke">
					<table data-ajax="handlerSearch" data-method="post"
								 data-id-field="id" 
								 data-unique-id="id" 
								 data-pagination="true"  
								 data-check-on-init="true" 
								 data-side-pagination="server" 
								 data-mobile-responsive="true" 
								 data-form-source=".table-editor-bespoke" 
								 data-form-target="#editor-form-bespoke" 
								 data-form-search="#search-form-bespoke" id="table-bespoke">
						<thead>
							<tr>
								<th data-checkbox="true"></th>
								<th data-field="id" data-visible="false"></th>
								<th data-field="createDate" 	data-align="center">预约时间</th>
								<th data-field="bespokeType" 	data-align="center" data-formatter="renderAudition">预约内容</th>
								<th data-field="promiseDate" 	data-align="center">诺访时间</th>
								<th data-field="commingDate" 	data-align="center" data-formatter="renderComming">到店情况</th>
								<th data-field="username" 		data-align="center">顾问</th>
								<th data-field="userid" 			data-align="center" data-visible="false">顾问ID</th>
								<th data-field="operator" 		data-align="center" data-width="70" data-formatter="renderOperator" data-table-target="bespoke">操作</th>
							</tr>
						</thead>
					</table>
				</form>
			</div>
		</div>
		
		<!-- TODO 基本信息 -->
		<div class="widget radius-bordered mgbt-xs-10">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 基本信息 </span>
			</div>
			<div class="widget-body editor-body-sm editor-body-customer">
				
			</div>
		</div>
		<!-- TODO 家长信息 -->
		<div class="widget radius-bordered">
			<div class="widget-header" id="">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> <a href="" data-toggle="collapse"> 家长信息 </a> </span>
				<div class="widget-buttons buttons-bordered">
					<button class="btn btn-default btn-sm" title="添加" 
									data-toggle="tooltip" data-placement="top" 
									data-tool="append" 
									data-template-id="#form-parent-template" 
									data-template-path="/miwawa/content/esm/customer/template.html" 
									data-template-target=".editor-body-parents"
									data-loaded="handlerParents">
						<i class="fa fa-plus"></i>
					</button>
					<button class="btn btn-default btn-sm" title="删除" 
									data-toggle="tooltip" data-placement="top" 
									data-tool="remove" data-template-target=".editor-body-parents"
									data-remove-action="/miwawa/{dbname}/parents/delete/{id}">
						<i class="fa fa-minus"></i>
					</button>
				</div>
				<div class="widget-buttons compact">
					<a href="" data-toggle="collapse"> <i class="fa fa-compress"></i> </a> 
				</div>
			</div>
			<div class="widget-body editor-body-sm editor-body-parents" id="editor-parentss">
				<!-- dynamic data -->
			</div>
		</div>
		<!-- TODO 宝宝信息 -->
		<div class="widget radius-bordered mgbt-xs-10">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> <a href="" data-toggle="collapse"> 宝宝信息 </a> </span>
				<div class="widget-buttons buttons-bordered">
					<button class="btn btn-default btn-sm" title="添加" 
									data-toggle="tooltip" data-placement="top" 
									data-tool="append" 
									data-template-id="#form-student-template" 
									data-template-path="/miwawa/content/esm/customer/template.html" 
									data-template-target=".editor-body-student"
									data-loaded="handlerStudent">
						<i class="fa fa-plus"></i>
					</button>
					<button class="btn btn-default btn-sm" title="删除" 
									data-toggle="tooltip" data-placement="top" 
									data-tool="remove" data-template-target=".editor-body-student">
						<i class="fa fa-minus"></i>
					</button>
				</div>
				<div class="widget-buttons compact">
					<a href="" data-toggle="collapse"> <i class="fa fa-compress"></i> </a> 
				</div>
			</div>
			<div class="widget-body editor-body-sm editor-body-student">
				<!-- dynamic data -->
			</div>
		</div>
		<!-- TODO 家庭住址 -->
		<div class="widget radius-bordered mgbt-xs-10">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> <a href="" data-toggle="collapse"> 家庭住址 </a> </span>
				<div class="widget-buttons buttons-bordered">
					<button class="btn btn-default btn-sm" title="添加" 
									data-toggle="tooltip" data-placement="top" 
									data-tool="append" 
									data-template-id="#view-address-template" 
									data-template-path="/miwawa/content/esm/customer/template.html" 
									data-template-target=".editor-body-address">
						<i class="fa fa-plus"></i>
					</button>
					<button class="btn btn-default btn-sm" title="删除" 
									data-toggle="tooltip" data-placement="top" 
									data-tool="remove" data-template-target=".editor-body-address">
						<i class="fa fa-minus"></i>
					</button>
				</div>
				<div class="widget-buttons compact">
					<a href="" data-toggle="collapse"> <i class="fa fa-compress"></i> </a> 
				</div>
			</div>
			<div class="widget-body editor-body-sm editor-body-address">
				<!-- dynamic data -->
			</div>
		</div>
		
	</div>
</div>

<!-- 提交表单 ========================================================= -->
<form id="update-form-customer" action="/miwawa/{dbname}/common/update" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Customer">
</form>
<form id="editor-form-parents" action="/miwawa/{dbname}/parents/save" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Parents"
			data-custom-action="true">
</form>
<form id="update-form-parents" action="/miwawa/{dbname}/parents/save" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Parents"
			data-custom-action="true">
</form>
<form id="editor-form-student" action="/miwawa/{dbname}/common/save" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Student">
			<input type="hidden" name="bean['status']" value="0">
			<input type="hidden" name="bean['invalid']" value="F">
			<input type="hidden" name="bean['customerId']" data-foreign-id>
</form>
<form id="update-form-student" action="/miwawa/{dbname}/common/update" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Student">
			<input type="hidden" name="bean['status']" value="0">
			<input type="hidden" name="bean['invalid']" value="F">
			<input type="hidden" name="bean['customerId']" data-foreign-id>
</form>
<form id="editor-form-address" action="/miwawa/{dbname}/common/update" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Address">
			<input type="hidden" name="bean['status']" value="0">
			<input type="hidden" name="bean['invalid']" value="F">
			<input type="hidden" name="bean['customerId']" data-foreign-id>
</form>
<form id="editor-form-selling" action="/miwawa/{dbname}/common/save" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Selling">
			<input type="hidden" name="bean['status']" value="0">
			<input type="hidden" name="bean['invalid']" value="F">
			<input type="hidden" name="bean['customerId']" data-foreign-id>
</form>
<form id="editor-form-bespoke" action="/miwawa/{dbname}/common/save" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Bespoke">
			<input type="hidden" name="bean['status']" value="0">
			<input type="hidden" name="bean['invalid']" value="F">
			<input type="hidden" name="bean['customerId']" data-foreign-id>
</form>
<!-- 查询表单 ========================================================= -->
<form id="search-form-parents" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Parents" 
			data-template-target=".editor-body-parents" data-template-id="#view-parents-template" 
			data-template-path="/miwawa/content/esm/customer/template.html">
			<input type="hidden" name="invalid" value="F" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="customerId" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" data-search-sort="status" data-search-order="desc">
</form>
<form id="search-form-student" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Student" 
			data-template-target=".editor-body-student" data-template-id="#view-student-template" 
			data-template-path="/miwawa/content/esm/customer/template.html">
			<input type="hidden" name="status" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="invalid" value="F" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="customerId" value="0" data-search-group="and" data-search-operation="EQ">
</form>
<form id="search-form-address" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Address" 
			data-template-target=".editor-body-address" data-template-id="#view-address-template" 
			data-template-path="/miwawa/content/esm/customer/template.html">
			<input type="hidden" name="status" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="invalid" value="F" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="customerId" value="0" data-search-group="and" data-search-operation="EQ">
</form>
<form id="search-form-selling" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.esm.entity.customer.Selling">
			<input type="hidden" name="status" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="invalid" value="F" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="customerId" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" data-search-sort="createDate" data-search-order="desc">
</form>
<form id="search-form-bespoke" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.esm.entity.view.AuditionView">
			<input type="hidden" name="customerId" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" data-search-sort="createDate" data-search-order="desc">
</form>

<script type="text/javascript">
<!--
	require(['domReady'],function(domReady){
		domReady(function () {
			require(['moment'], function(moment){
				$moment = moment;
			});
 			initData();
    });
	});
	
	var $moment;
	var promisedate;
	
	var customer = jQuery("#table-allCustomer, #table-salescustomer, #table-bespokecustomer").data("selectedRow");
	function initData(){
		jQuery(":hidden[name='customerId'],[data-foreign-id]").val(customer.customerid);
		jQuery(".editor-body-customer").loadform("load", {
			templateId: "#view-customer-template",
			templatePath: "/miwawa/content/esm/select/template.html",
			templateData: customer,
			templateHelper:{
				"_market":function(value){
					require(['jquery.plugin.form'],function(form){
						form.chosen("#markets").select("#markets",{},function(chosen){
							jQuery("option[value='"+value+"']",chosen).attr("selected","selected").trigger('chosen:updated');
						});
					});
				},
				"_source":function(value){
					require(['jquery.plugin.form'],function(form){
						form.chosen("#sources").select("#sources",{},function(chosen){
							jQuery("option[value='"+value+"']",chosen).attr("selected","selected").trigger('chosen:updated');
						});
					});
				}
			}
		});
		
 		jQuery("#search-form-parents").loadform("many",{
			"_relation":function(value,index){
				require(['jquery.plugin.form'],function(form){
					form.chosen("#relation-"+index).select("#relation-"+index,{},function(chosen){
						jQuery("option[value='"+value+"']",chosen).attr("selected","selected").trigger('chosen:updated');
					});
				});
			}		
		}); 
		
		jQuery("#search-form-student").loadform("many",{
			"_sex":function(value,index){
				require(['jquery.plugin.form'],function(form){
					form.chosen("#sex-"+index).select("#sex-"+index,{},function(chosen){
						jQuery("option[value='"+value+"']",chosen).attr("selected","selected").trigger('chosen:updated');
					});
				});
			}
		});

		jQuery("#search-form-address").loadform("many",{
			"region":function(value){
				require(['jquery.plugin.form'],function(form){
					form.tooltip("[data-toggle='tooltip']");

					jQuery(".form-editor", ".editor-body-address").each(function(index,address){
						var _region = jQuery("#province,#prefecture,#county", address);
						form.chosen(_region).region(_region[2],{"pid":"310100"},function(chosen){
							jQuery("option[value='"+value+"']",chosen).attr("selected","selected").trigger('chosen:updated');
						});
					});
				});
			}
		});

		jQuery("#table-selling").bootstrapTable();
		
		jQuery("#table-bespoke").bootstrapTable({
			onLoadSuccess:function(data){
				require(['jquery.plugin.form'],function(form){
					form.tooltip("[data-toggle='tooltip']");
					form.datepicker('.date-selector',{format:'YYYY-MM-DD'});
					form.datepicker('.date-controller',{format:'YYYY-MM-DD'});		
					form.datepicker('#date-birthday',{format:'YYYY-MM-DD'});		
				});
				
			}
		});
	}
	
	function judge(form){
		var parentForm = jQuery(".form-editor-parents");
		parentForm.each(function(i,n){
		 	if(i<=0){
				jQuery('#parStatus',form).val('true');
		 	}else{
				jQuery('#parStatus',form).val('false');
		 	} 
		});
 	}
	
	function intercept(target){
		jQuery.ajaxsend({
			url:"/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
			data:{
	       		"entityClass":"com.app.miwawa.api.esm.entity.customer.Bespoke",
	       	 	"and[0][operator]": "EQ",
	          "and[0][fieldName]": "customerId",					
	          "and[0][fieldValue]": customer.customerid
			},
			success:function(data){
				var bespoke = data.rows;
				for(var i=0;i<bespoke.length;i++){
					var come = bespoke[i].commingState;
					if(come == ""){
						jQuery.message.alert("您有未处理的诺访，请在到店情况确认是否到店后，才可新增诺访。");
						return;
					}
				}
				handlerAddRow(target);
			}
		});
	}
	
	//新增家长检验手机号
	function holdup(botton, table, form){
		if((/^1(3|4|5|7|8)\d{9}$/).test(jQuery("#mobile",form).val()) == false){
			jQuery.message.alert("请输入正确的手机号");
			return;
		}else{
			jQuery.ajaxsend({
				url:"/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
				data:{
							"entityClass":"com.app.miwawa.api.esm.entity.customer.Parents",
							"and[0][operator]": "EQ",
					    "and[0][fieldName]": "mobile",				
		          "and[0][fieldValue]": jQuery("#mobile",form).val()
				},
				success:function(data){
					var counts = data.rows;
					if(counts.length > 0){
						jQuery.message.alert("此手机号已存在，请更换。");
						return;
					}
					jQuery("#customerId",form).val(customer.customerid);
					handlerSubmit(botton, table, form);
				}
			});
		}
	}
	
	//更换手机号校验
	function changeMobile(botton, table, form){
		if((/^1(3|4|5|7|8)\d{9}$/).test(jQuery("#mobile",form).val()) == false){
			jQuery.message.alert("请输入正确的手机号");
			return;
		}else{
			jQuery.ajaxsend({
				url:"/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
				data:{
							"entityClass":"com.app.miwawa.api.esm.entity.customer.Parents",
							"and[0][operator]": "EQ",
					    "and[0][fieldName]": "mobile",				
		          "and[0][fieldValue]": jQuery("#mobile",form).val()
				},
				success:function(data){
					var counts = data.rows;
					if(counts.length > 0){
						for(var l = 0; l<counts.length; l++){
							if(counts[l].id != jQuery("#parentid",form).val()){
								jQuery.message.alert("此手机号已存在，请更换。");
								return;
							}
						}
						jQuery.ajaxsend({
							  url:"/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
								data:{
									"entityClass":"com.app.miwawa.api.esm.entity.customer.Parents",
									"bean":{"id":jQuery("#parentid",form).val(),
													"relation":jQuery("form[name='editParents'] select[name='relation']").val(),
													"name":jQuery("#name",form).val(),
													"mobile":jQuery("#mobile",form).val()
									}
								},
								success:function(){
									handlerSubmit(botton, table, form);
								}
						});
					}else{
						jQuery.ajaxsend({
							  url:"/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
								data:{
									"entityClass":"com.app.miwawa.api.esm.entity.customer.Parents",
									"bean":{"id":jQuery("#parentid",form).val(),
													"relation":jQuery("form[name='editParents'] select[name='relation']").val(),
													"name":jQuery("#name",form).val(),
													"mobile":jQuery("#mobile",form).val()
									}
								},
								success:function(){
									handlerSubmit(botton, table, form);
								}
						});
					}
				}
			});
		}
	}
	
	// 新增家长输入表单后的回调
	function handlerParents(options,data){
		if(options.id){
			var last = jQuery(".form-editor:last", ".editor-body-parents");
			last.append('<input type="hidden" name="id" value="'+options.id+'"/>');
			last.data("entity","com.app.miwawa.api.esm.entity.customer.Parents");
		}else{
			require(['jquery.plugin.form'],function(form){
				var relation = jQuery("#relation",".form-editor-parents-"+options.index);
				if(relation.length > 0){
					form.chosen(relation).select(relation);
					form.tooltip("[data-toggle='tooltip']");
				}
			});
		} 
	}
	
	// 新增宝宝输入表单后的回调
	function handlerStudent(options){
		if(options.id){
			// 更新表单信息
			var last = jQuery(".form-editor:last", ".editor-body-student");
			last.append('<input type="hidden" name="id" value="'+options.id+'"/>');
			last.data("entity","com.app.miwawa.api.esm.entity.customer.Student");
			
			// 更新头像信息
			var avatar = last.find(":hidden[name='avatar']").next("a");
			avatar.data("update",true);
			avatar.data("pk",options.id);
			avatar.data("entity","com.app.miwawa.api.esm.entity.customer.Student");
		}else{
			require(['jquery.plugin.form'],function(form){
				form.datepicker(jQuery(".date-selector",".form-editor-student-"+options.index));
				form.tooltip("[data-toggle='tooltip']");
			});
		}
	}
	
	// 新增地址输入表单后的回调
	function handlerAddress(options){
		if(options.id){
			// 更新表单信息
			var last = jQuery(".form-editor:last", ".editor-body-address");
			last.find(":hidden[name='id']").val(options.id);
		}
	}
	
	// 打开模态窗口
	function handlerModal(trigger, url, before, callback){
		jQuery("#extension-modal").removeClass("modal-middle").data("url",url).data("onshow",function(){
			if(before) window[before](trigger);
		}).data("onhidden",function(){
			if(callback) window[callback](trigger);
		}).modal("show");
	}
	
	// 试听排班模态窗口
	function handlerAudition(trigger, rowid, promise){
		promisedate = promise;	
		jQuery("#extension-modal").removeClass("modal-middle").data("selectedRow", jQuery("#table-bespoke").bootstrapTable("getRowByUniqueId",rowid))
															.data("url","content/esm/select/dialog/classes.html").data("onshow",function(){
																var $table = jQuery("#table-classes");
																if($table.hasClass("table")){
																	$table.bootstrapTable("refresh");
																}else{
																	$table.bootstrapTable();
																}
															}).modal("show");
	}
	
	// 到店
	function handlerComming(trigger, rowid){
		require(["moment"],function(moment){
			jQuery.ajaxsend({
				url: "/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
				data: {
					"entityClass":"com.app.miwawa.api.esm.entity.customer.Bespoke",
					"bean":{"id":rowid,
									"commingDate":moment().format("YYYY-MM-DD HH:mm"),
									"commingState":"0"
					       }
				},
				success:function(data){
					jQuery.ajaxsend({
						url:"/miwawa/{dbname}/customer/audition/edit/{bespokeid}".replace("{dbname}",dbname).replace("{bespokeid}",rowid),
						success:function(){
							jQuery("[data-toggle='tooltip']","#table-bespoke").tooltip();
							jQuery("#table-bespoke").bootstrapTable("refresh");
						}
					});
				}
			});
		});
	}
	
	// 未到店
	function notTo(trigger, rowid){
		require(["moment"],function(moment){
			jQuery.ajaxsend({
				url: "/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
				data: {
					"entityClass":"com.app.miwawa.api.esm.entity.customer.Bespoke",
					"bean":{"id":rowid, "commingState":"1"}
				},
				success:function(data){
					jQuery.ajaxsend({
						url:"/miwawa/{dbname}/customer/audition/edit/{bespokeid}".replace("{dbname}",dbname).replace("{bespokeid}",rowid),
						success:function(){
							jQuery("[data-toggle='tooltip']","#table-bespoke").tooltip();
							jQuery("#table-bespoke").bootstrapTable("refresh");
						}
					});
				}
			});
		});
	}
	
	// 渲染列表试听列
	function renderAudition(value, row, index){
		if(value == "试听" && row.classes){
			return value+" "+row.classes;
		}else if(value == "试听" && row.id != "new" && row.commingState == ""){
			return value+" <a class='btn btn-success btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='试听' onclick='handlerAudition(this, \""+row.id+"\", \""+row.promiseDate+"\")'> <i class='icon-earphones'></i> </a>";			
		}else if(value == "试听" && row.commingState != ""){
			return value;
		}else{
			return value;			
		}
	}
	
	// 渲染列表到访列
	function renderComming(value, row, index){
		if(row.id == "edit"){
			return ""
		}else{
			if(row.commingState == "" ){
				return "<a class='btn btn-success btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='到店' onclick='handlerComming(this, \""+row.id+"\")'> <i class='fa fa-check'></i> </a>&nbsp;到店"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='btn btn-warning btn-xs btn-table-op' data-toggle='tooltip' data-placement='top' title='未到店' onclick='notTo(this, \""+row.id+"\")'> <i class='fa fa-times'></i> </a>&nbsp;未到店";			
			}else if(row.commingState == "1"){
				return "未到店";			
			}else if(row.commingState == "0"||row.commingState == "2"){
				return "已到店";			
			}else{
				return "";
			}
		}
	}
	
	/* ===================================== */
	/* 表格 行编辑 开始 */
	/* ===================================== */
	// 添加行编辑
	function handlerAddRow(target){
		var $table = jQuery(target);
		
		if(!hasEditorRow()){
			var rowdata = {
				"table-selling":{
					id:'new',
	        createDate: createInput('createDate', 'text', 		'ext-time'),
	        intention: 	createInput('intention', 	'text', 		'ext-rate', '3'),
	        content: 		createInput('content', 		'text', 		'ext-text'),
	        nextDate: 	createInput('nextDate', 	'text', 		'ext-date', $moment().add(3, 'd').format("YYYY-MM-DD HH:mm")),
	        username:		createInput('username', 	'readonly', 'ext-text', username),
	        operator: 	createButton(target)
				},
				"table-bespoke":{
					id:'new',
          createDate: 	createInput('createDate', 	'text', 		'ext-time'),
          bespokeType: 	createInput('bespokeType', 	'select', 	'ext-text'),
          promiseDate: 	createInput('promiseDate',	'text', 		'ext-date'),
          username:			createInput('username', 		'readonly', 'ext-text', username),
          operator: 		createButton(target)
				}
			};
			
			$table.bootstrapTable('insertRow', { index: 0, row: rowdata[target.replace("#","")] });
			
			setTimeout(function(){
				require(['jquery.plugin.form'],function(form){
					form.rating(".ext-rate");
					form.datepicker(".ext-time",{format: "YYYY-MM-DD HH:mm"});
					form.datepicker(".ext-date",{format: "YYYY-MM-DD HH:mm"});
				});
			},0);
		}
		
		// 创建输入框
		function createInput(name, type, ext, value){
			switch(type){
				case 'text': 
					var input = "";
					if(value != undefined){
						input = '<input name="'+name+'" type="'+type+'" id="'+name+'" class="form-control '+ext+'" value="'+value+'">';
					}else{
						input = '<input name="'+name+'" type="'+type+'" id="'+name+'" class="form-control '+ext+'">';
					}
					return input;
				case 'readonly': 
					input = '<input name="'+name+'" type="'+type+'" class="form-control '+ext+'" value="'+value+'" readonly="readonly">';
					if(name=="username"){
						input+='<input name="userid" type="hidden" value="'+userid+'">';
					}
					return input;
				case 'select': 
					var select='<select name="'+name+'" id="'+name+'" class="form-control" style="width:80px;">'
									  +	'<option value="看店">看店</option>'
									  +	'<option value="试听">试听</option>'
									  +	'<option value="签约">签约</option>'
									  +	'<option value="付款">付款</option>'
									  +'</select>';
				return select;
			}
		}
		
		// 创建按钮
		function createButton(target){
			return ""
						+"<span class='table-editor-tools'>"
						+"	<button type='button' class='btn btn-danger  btn-xs' onclick='handlerCancelRow(\""+target+"\");'><i class='icon ion-close-circled'></i></button>"
						+"	<button type='button' class='btn btn-success btn-xs' onclick='handlerSubmitRow(\""+target+"\",\"validated\");'><i class='icon ion-checkmark-circled'></i></button>"
						+"</span>";
		}
		
		// 判断是否有编辑行
		function hasEditorRow(){
			return $table.bootstrapTable('getRowByUniqueId', 'new') ? true : false;
			}
	}
	
	//修改诺访记录
	var bespoke;
	function editBespoke(trigger,index){
		jQuery("[data-toggle='tooltip']","#table-bespoke").tooltip("destroy");
		
		var _opts = jQuery(trigger).data();
		bespoke = _opts.rowdata;
		
		jQuery("#table-bespoke").bootstrapTable("updateRow",{
			index: index,
			row: editRows(_opts.rowdata, _opts.tableSource)
		});
		
		//修改诺访
		function editRows(row,target){
			return {
				id:   				"edit",
				createDate: 	row.createDate,
				bespokeType: 	foundInput('bespokeType'),
				promiseDate: 	row.promiseDate,
				username: 		row.username,
				userid: 			row.userid,
	      operator: 		foundButton()
			};
		}
		
		// 创建诺访类型选择框
		function foundInput(name){
	    var select = '<select name="'+name+'" id="'+name+'" class="form-control" style="width:80px;">'
						     + '<option value="看店">看店</option>'
								 + '<option value="试听">试听</option>'
								 + '<option value="签约">签约</option>'
								 + '<option value="付款">付款</option>'
								 + '</select>';
			return select;
		}
		
		// 创建更新诺访记录后的提交及取消按钮
		function foundButton(){
			return ""
				+"<span class='table-editor-tools'>"
				+"	<button type='button' class='btn btn-danger  btn-xs' onclick='handCanced()' title='取消'><i class='icon ion-close-circled'></i></button>"
				+"	<button type='button' class='btn btn-success btn-xs' onclick='handSured()' title='确认'><i class='icon ion-checkmark-circled'></i></button>"
				+"</span>";
		}
		
	}
	
	//更新诺访记录
 	function handSured(){
	 	jQuery.ajaxsend({
	 		url:"/miwawa/{dbname}/common/update".replace("{dbname}", dbname),
	 		data:{
	 				"entityClass":"com.app.miwawa.api.esm.entity.customer.Bespoke",
					"bean":{"id":bespoke.id,"bespokeType":jQuery("#bespokeType").val()}
	 		},
	 		success:function(data){
	 			jQuery.ajaxsend({
					url:"/miwawa/{dbname}/customer/bespoke/edit/{bespokeid}".replace("{dbname}", dbname).replace("{bespokeid}",bespoke.id),
					success:function(){
						jQuery("#table-bespoke").bootstrapTable("refresh");
					}
				});
	 		}
		});
	}
 	
	//取消更新诺访记录
	function handCanced(){
		jQuery("#table-bespoke").bootstrapTable("refresh");
	}
	
	
	//跟进输入框必填
	function validated(){
		if(jQuery("#content").val()!=undefined){
			if(jQuery("#createDate",".table-editor-selling").val()==""){
				jQuery.message.alert("请输入通话时间！");
				return false;
			}else if(jQuery("#content").val()==""){
				jQuery.message.alert("请输入跟踪情况！");
				return false;
			}else if(jQuery("#content").val().length > 100){
				jQuery.message.alert("跟踪情况字数限制在100以内！");
				return false;
			}else if(jQuery("#nextDate").val()==""){
				jQuery.message.alert("请输入下次沟通时间！");
				return false;
			}else{
				return true;			
			}
		}else{
			if(jQuery("#createDate",".table-editor-bespoke").val()==""){
				jQuery.message.alert("请输入预约时间！");
				return false;
			}else if(jQuery("#promiseDate").val()==""){
				jQuery.message.alert("请输入诺访时间！");
				return false;
			}else{
				var selling = [];
				selling.push({
					"content":jQuery("#bespokeType").val(),
					"intention":"5",
					"createDate":jQuery("#createDate",".table-editor-bespoke").val(),
					"nextDate":jQuery("#promiseDate").val(),
					"userid":customer.userid,
					"username":customer.username,
					"customerId":customer.customerid,
					"status":"0",
					"invalid":"F"
				});
				jQuery.ajaxsend({
					url:"/miwawa/{dbname}/common/save".replace("{dbname}",dbname),
					data:{
						"entityClass":"com.app.miwawa.api.esm.entity.customer.Selling",
						"beans":selling
					},
					success:function(data){
						jQuery("#table-bespoke").bootstrapTable('refresh');
						jQuery("#table-selling").bootstrapTable('refresh');
						return true;		
					}
				});
			}
		}
	}
	
	//删除环信
	function deleteEasemob(customerid){
		jQuery.ajaxsend({
			url:"/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
			async:false,
			data:{
	       		"entityClass" : "com.app.miwawa.api.esm.entity.customer.Parents",
	          "and[0][operator]": "EQ",
	          "and[0][fieldName]": "customerId",					
	          "and[0][fieldValue]": customerid
	  	},
	  	success:function(data){
	  		var parents = data.rows;
	  		for(var i=0; i<parents.length; i++){
	  			jQuery.ajaxsend({
	  				url:("/miwawa/{dbname}/parents/delete/{id}".replace("{dbname}", dbname)).replace("{id}",parents[i].id),
	  			});
	  		}
	  	}
		});
	}
	
	//再次跟进
	function followAgain(createDate, promiseDate ,customerId){
		jQuery.ajaxsend({
			url : "/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
			data : {
	       		"entityClass" : "com.app.miwawa.api.esm.entity.customer.Bespoke",
	          "and[0][operator]": "EQ",
	          "and[0][fieldName]": "customerId",					
	          "and[0][fieldValue]": customerId,
	          "order[0][sort]": "promiseDate",
	          "order[0][order]":"desc"
	  	},
	  	success : function(data) {
	  		var bespokeList = data.rows;
				jQuery.ajaxsend({
					url:"/miwawa/{dbname}/common/update".replace("{dbname}",dbname),
					data:{
				       	"entityClass" : "com.app.miwawa.api.esm.entity.customer.Bespoke",
				       	"bean":{"id":bespokeList[0].id,"commingState":"2"}
				  },
				  success : function(data) {
				  	jQuery("#table-customer").bootstrapTable("refresh");
				  }
				});
	  	}
		});
	}
	
	//宝宝生日检查
	function checkBirthday(botton, table, form){
		var birthday = $moment(jQuery("#birthday",form).val());
		var nowDate = $moment().format("YYYY-MM-DD");
		if(!$moment(nowDate).isAfter(birthday)){
			jQuery.message.alert("宝宝生日填写有误,请重新填写");
			return;
		}
		handlerSubmit(botton, table, form);
	}
//-->
</script>